package com.crp.unitalinforbaseplat.app.sys.service.impl;

import com.anhuishangjue.praduationppmsystem.Utils.EmptyUtil;
import com.crp.unitalinforbaseplat.app.core.Message;
import com.crp.unitalinforbaseplat.app.core.MessageUtil;
import com.crp.unitalinforbaseplat.app.sys.dao.*;
import com.crp.unitalinforbaseplat.app.sys.entity.*;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.PageBean;
import com.crp.unitalinforbaseplat.app.sys.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    private UserMapper sysUserInfoMapper;

    @Override
    public Message saveOrUpdate(Role role) {
        if(EmptyUtil.Companion.isNullOrEmpty(role.getId())){
            Role check = roleMapper.selectByName(role.getName());
            if(check!= null){
                return MessageUtil.createMessage(false,"角色已经存在");
            }else{
                roleMapper.insertSelective(role);
                return MessageUtil.createMessage(true,"新增成功");
            }
        }else{
            Role check = roleMapper.selectByPrimaryKey(role.getId());
            if(check == null){
                return MessageUtil.createMessage(false,"角色不存在");
            }else{
                roleMapper.updateByPrimaryKeySelective(role);
                return MessageUtil.createMessage(true,"更新成功");
            }
        }
    }

    @Override
    public PageBean<Role> list(PageBean<Role> rolePageBean) {
        rolePageBean.setData(roleMapper.list(rolePageBean));
        return rolePageBean;
    }

    @Override
    public Message delete(Long[] ids) {
        for(Long id:ids){
            roleMapper.deleteByPrimaryKey(id.intValue());
        }
        return MessageUtil.createMessage(true,"删除成功！");
    }

    @Override
    public Message setUsers(Long[] userIds, Long roleId) {
        return null;
    }

    @Override
    public Message setPermission(Long[] PermissionIds, Long roleId) {
        return null;
    }

    @Override
    public Role updateInit(Long id) {
        return roleMapper.selectByPrimaryKey(id.intValue());
    }


    @Override
    public List<SelectEntity> listUserInfo(User sysUserInfo) {
        return sysUserInfoMapper.listUserInfo(sysUserInfo);
    }

    @Override
    public Message roleAddUser(Long[] userInfo, Long roleId) {
        UserRole role = null;
        List<UserRole> userRoles = new ArrayList<UserRole>();
        if(userInfo != null){
            for (Long userId: userInfo) {
                role = new UserRole();
                role.setUserId(userId.toString());
                role.setRoleId(roleId.toString());
                userRoles.add(role);
            }
            userRoleMapper.deleteByRoleId(roleId);
            userRoleMapper.insertBatch(userRoles);
        }else{
            userRoleMapper.deleteByRoleId(roleId);
        }
        return MessageUtil.createMessage(true,"用户添加成功");
    }

    @Override
    public List<SelectEntity> listPermission(Permission permission) {
        return permissionMapper.listPermission(permission);
    }

    @Override
    public Message roleAddPermission(Long[] permission, Long id) {
        RolePermission rolePermission = null;
        List<RolePermission> rolePermissions = new ArrayList<RolePermission>();
        if(permission != null){
            for (Long permissionId: permission) {
                rolePermission = new RolePermission();
                rolePermission.setPermissionId(permissionId.toString());
                rolePermission.setRoleId(id.toString());
                rolePermissions.add(rolePermission);
            }
            rolePermissionMapper.deleteByRoleId(id);
            rolePermissionMapper.insertBatch(rolePermissions);
        }else{
            rolePermissionMapper.deleteByRoleId(id);
        }
        return MessageUtil.createMessage(true,"权限添加成功");
    }
}
